package ui;

import java.text.SimpleDateFormat;

import control.SystemUIControl;
import sniff.pack.CommonIPChildPacket;
import sniff.pack.CommonIPPacket;
import sniff.pack.CommonPacket;
import sniff.pack.MyTCPPacket;
import ui.other.YPBorderFactory;
import ui.widget.ColorXTransparentPanel;
import ui.widget.TxtPanel;

public class SimpleMsgItem extends ColorXTransparentPanel{
	private CommonPacket pack;
	public SimpleMsgItem(CommonPacket pack) {
		super(SystemUIControl.getFrameWidth()-30,30);
		this.pack =pack;	
		init();
	}
	private void init() {
		String source = pack.getSourceMAC();
		String destination = pack.getDestinationMAC();
		String protocol = pack.getType();
		if(pack.getType().equals("IPV4")||pack.getType().equals("IPV6")) {
			CommonIPPacket ipPack = (CommonIPPacket)pack;
			source = ipPack.getSourceIP();
			destination = ipPack.getDestinationIP();
			if(protocol!=null&&!protocol.equals("")) {
				protocol = ipPack.getProtocol();
			}
			if(protocol.equals("TCP")) {
				CommonIPChildPacket ipCPack = ipPack.getChildPacket();
				if(ipCPack instanceof MyTCPPacket) {
					MyTCPPacket tcpPack = (MyTCPPacket)ipCPack;
					if(tcpPack.getChildPacket()!=null) {
						protocol = "HTTP";
					}
				}else {
					System.out.println("error in SimpleMsgItem TCP");
				}
			}
		}
		String dateStr =new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(pack.getDate());
		add(new TxtPanel(80,30,pack.getNo()+""));
		add(new TxtPanel(150,30,dateStr));
		add(new TxtPanel(280,30,source));
		add(new TxtPanel(280,30,destination));
		add(new TxtPanel(100,30,protocol));
		add(new TxtPanel(70,30,pack.getLenght()+""));
		add(new TxtPanel(300,30,pack.getInfo()));
		setBorder(YPBorderFactory.getUnderLineBorder());
	}
	public CommonPacket getPack() {
		return pack;
	}
	public void setPack(CommonPacket pack) {
		this.pack = pack;
	}
}
